linux替换sed r n为 n,sed的:如何使用 “\ r” “\ n” 替换CR和/或LF,所以任何文件将在一行... 您所在的位置:网站首页 sed 替换/r linux替换sed r n为 n,sed的:如何使用 “\ r” “\ n” 替换CR和/或LF,所以任何文件将在一行...

linux替换sed r n为 n,sed的:如何使用 “\ r” “\ n” 替换CR和/或LF,所以任何文件将在一行...

2024-06-08 01:26| 来源: 网络整理| 查看: 265

可以在sed中合并行,但是我个人认为需要改变换行符表示现在是放弃sed并使用更强大的语言的时候了。你想要的是perl的一行:蟒蛇

perl -e 'undef $/; while () { s/\n/\\n/g; s/\r/\\r/g; print $_, "\n" }'

或12行:

#! /usr/bin/python

import fileinput

from sys import stdout

first = True

for line in fileinput.input(mode="rb"):

if fileinput.isfirstline() and not first:

stdout.write("\n")

if line.endswith("\r\n"): stdout.write(line[:-2] + "\\r\\n")

elif line.endswith("\n"): stdout.write(line[:-1] + "\\n")

elif line.endswith("\r"): stdout.write(line[:-1] + "\\r")

first = False

if not first: stdout.write("\n")

或10行的C做的工作,但后来一大堆更多,因为你必须处理argv自己:

#include

void process_one(FILE *fp)

{

int c;

while ((c = getc(fp)) != EOF)

if (c == '\n') fputs("\\n", stdout);

else if (c == '\r') fputs("\\r", stdout);

else putchar(c);

fclose(fp);

putchar('\n');

}

int main(int argc, char **argv)

{

FILE *cur;

int i, consumed_stdin = 0, rv = 0;

if (argc == 1) /* no arguments */

{

process_one(stdin);

return 0;

}

for (i = 1; i < argc; i++)

{

if (argc[i][0] == '-' && argc[i][1] == 0)

{

if (consumed_stdin)

{

fputs("cannot read stdin twice\n", stderr);

rv = 1;

continue;

}

cur = stdin;

consumed_stdin = 1;

}

else

{

cur = fopen(ac[i], "rb");

if (!cur)

{

perror(ac[i]);

rv = 1;

continue;

}

}

process_one(cur);

}

return rv;

}



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有